package enterprises

import grails.test.*

class ConsumerIntegrationTests extends GroovyTestCase {
	
	static final String TEST_CONSUMER = "TestConsumer"
	static final String ANOTHER_NAME = "ANOTHER_NAME"
	
    protected void setUp() {
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }

    void testSaveConsumer() {		
		def typeConsumer = new TypeConsumer(name:"test_consumer")
		typeConsumer.save()
		
		def consumer = new Consumer(name:TEST_CONSUMER, typeConsumer:typeConsumer)		
		
		consumer.validate()
		
		println consumer.errors
		
		assertNotNull consumer.save()
		assertNotNull consumer.id
		
		def foundConsumer = Consumer.get(consumer.id)
		assertEquals TEST_CONSUMER, foundConsumer.name
    }		
	
	void testConsumerWithProduction() {
		def consumer = new Consumer(name:TEST_CONSUMER)
		def anotherConsumer = new Consumer(name:"Another_Consumer")
		
		def beer = new Production(name:"Beer")
		def fish = new Production(name:"Fish")
		
		consumer.addToProductions(beer)
		consumer.addToProductions(fish)
		
		anotherConsumer.addToProductions(beer)
		
		assertEquals 2, consumer.productions.size()		
		
		consumer.save()
		
		def found = Consumer.get(consumer.id)
		assertEquals 2, consumer.productions.size()
		assertEquals 2, beer.consumer.size()	
	}
}
